using UnityEngine;
using System.Collections;

public class HeliRobot : Enemy {

    public Transform laserSpawner1;
    public Transform laserSpawner2;
    public Transform laserPrefab;
    private Transform target;
    public GameObject heliRobotModel;

    public override void Start()
    {
        base.Start();

        speed = 5;
        hp = 100;
        damage = 10;
        damageDelay = 1;
        damageTimer = 1;

        foreach(Transform children in player.GetComponentsInChildren<Transform>())
        {
            if(children.name == "Personagem")
            {
                target = children;
            }
        }

        heliRobotModel.animation["Fly"].wrapMode = WrapMode.Loop;
        heliRobotModel.animation.Play();
    }

    public override void Attack()
    {
        base.Attack();

        transform.LookAt(new Vector3(target.position.x, target.position.y, target.position.z));

        int distanceDiff = Mathf.Abs((int)gameObject.transform.position.z - (int)player.position.z);

        if(distanceDiff < 10)
        {
            Shoot();
        }
        else
        {
            transform.Translate((Vector3.forward * speed) * Time.deltaTime);
        }
    }

    public void Shoot()
    {
        damageTimer += Time.deltaTime;

        if(damageTimer >= damageDelay)
        {
            Instantiate(laserPrefab, new Vector3(laserSpawner1.transform.position.x, laserSpawner1.transform.position.y, laserSpawner1.transform.position.z), transform.rotation);
            Instantiate(laserPrefab, new Vector3(laserSpawner2.transform.position.x, laserSpawner2.transform.position.y, laserSpawner2.transform.position.z), transform.rotation);
            damageTimer = 0;
        }
    }
}
